<template>
  <div class="follow-up-card">
    <!-- 第一行：随访记录标题 -->
    <div class="card-title">{{activeTab == 'pharmacy-py'?'配药':'发药'}}历史</div>

    <!-- 折叠面板 -->
    <el-collapse
      v-model="activeNames" v-if="patientDetail && patientDetail.length > 0"
      class="follow-up-collapse" accordion
      v-show="isExpanded"
    >
      <el-collapse-item
        :name="index" 
        v-for="(item, index) in patientDetail"
        :key="index"
      >
        <template #title>
          <div class="collapse-title">
            <span class="diagnosis">{{activeTab == 'pharmacy-py' ? item.dispenseTime:item.deliveryTime}}</span>
          </div>
        </template>
        <div
          class="collapse-content"
          v-for="(items, index) in item.rxList"
          :key="index"
        >
          <div
            class="collapse-title info-row"
            style="margin-top: 10px"
            v-if="items.rxType == '1'"
          >
            <span class="diagnosis">{{ items.rxName }}</span>
          </div>

          <!-- 成药处方 -->
          <div style="" v-if="items.rxType == '1'">
            <div class="info-row" v-for="(itemx, index) in items.detailList">
              <span class="label"  v-if="itemx.drugCategory != '3'"
                >{{ itemx.drugName }} {{ itemx.drugDosage
                }}{{ itemx.drugDosageUnitText }}*{{ itemx.minPackQty }}片/{{
                  itemx.packUnitText
                }}<span style="margin-left: 5px;">{{ itemx.dispenseQty
                }}{{ itemx.totalOutQtyUnitText }}</span></span
              >
              <span class="label"  v-if="itemx.drugCategory == '3'"
                >{{ itemx.drugName }} {{ itemx.minPackQty
                }}{{ itemx.minPackUnitText }}/{{
                  itemx.packUnitText
                }}   {{ itemx.dispenseQty
                }}{{ itemx.totalOutQtyUnitText }}
                </span
              >
            </div>
          </div>

          <!-- 中药处方 -->
          <div
            class="collapse-title info-row"
            style="margin-top: 10px"
            v-if="items.rxType == '3'"
          >
            <span class="diagnosis">{{items.rxName}}</span>
          </div>  
          <div class="collapse-title info-row" v-if="items.rxType == '3'">
            <div>本次{{activeTab == 'pharmacy-py'?'配':'发'}}：{{items.tcmInfo.currentDeliveryQty}}剂</div>
          </div>
          <div class="tcm-box" style="" v-if="items.rxType == '3'">
            <div  v-for="itema,index in items.tcmInfo.detailList" :key="itema.drugId" style="width:33.3%">
                <span style="margin-right: 10px;">{{itema.drugName}}<span style="margin-left: 10px;">{{itema.dosage}}{{itema.dosageUnitText}}</span></span>
            </div>
          </div>
         
          <div
            class="collapse-title info-row"
            style="margin-top: 10px"
            v-if="items.rxType == '2'"
          >
            <span class="diagnosis">{{items.rxName}}</span>
          </div>
          <!-- 输液处方 -->

          <div
            v-if="items.rxType == '2'"
            v-for="(itemx, indexs) in items.groupList"
            :key="indexs"
          >
          <!-- drugDosage+drugDosageUnitText*minPackQty+minPackUnitText/packUnitText -->
            <div>{{ itemx.groupName }}</div>
            <div class="info-row" v-for="(itemds, index) in itemx.detailList">
              <span class="label" v-if="itemds.drugCategory != '3'"
                >{{ itemds.drugName }} {{ itemds.drugDosage
                }}{{ itemds.drugDosageUnitText }}*{{ itemds.minPackQty }}{{itemds.minPackUnitText}}/{{
                  itemds.packUnitText
                }}   {{ itemds.dispenseQty
                }}{{ itemds.totalOutQtyUnitText }}</span
              >
              <span class="label" v-if="itemds.drugCategory == '3'"
                >{{ itemds.drugName }} {{ itemds.minPackQty
                }}{{ itemds.minPackUnitText }} / {{
                  itemds.packUnitText
                }}   {{ itemds.dispenseQty
                }}{{ itemds.totalOutQtyUnitText }}</span>
            </div>
          </div>
        </div>
        <div style="margin-top: 10px">
          <!-- <div class="info-row">
            <span class="label">{{activeTab == 'pharmacy-py'?'配药':'发药'}}人员：{{ item.dispenseUserName }}</span>
          </div>
          <div class="info-row">
            <span class="label">{{activeTab == 'pharmacy-py'?'配药':'发药'}}时间：{{ item.dispenseTime }}</span>
          </div> -->

          <div class="info-row" v-if="item.dispenseUserName">
            <span class="label">配药人员：{{ item.dispenseUserName }}</span>
          </div>
          <div class="info-row" v-if="item.dispenseTime">
            <span class="label">配药时间：{{ item.dispenseTime }}</span>
          </div>
          <div class="info-row" v-if="item.deliveryUserName && activeTab == 'pharmacy-fy'">
            <span class="label">发药人员：{{ item.deliveryUserName }}</span>
          </div>
          <div class="info-row" v-if="item.deliveryTime && activeTab == 'pharmacy-fy'">
            <span class="label">发药时间：{{ item.deliveryTime }}</span>
          </div>


          <div class="info-row"  v-if="item.deliveryStatus == '3'">
            <span class="label">驳回人员：{{ item.deliveryUserName }}</span>
          </div>
          <div class="info-row"  v-if="item.deliveryStatus == '3'">
            <span class="label">驳回时间：{{ item.deliveryTime }}</span>
          </div>
          <div class="info-row"  v-if="item.deliveryStatus == '3'">
            <span class="label">驳回原因：{{ item.refuseReason }}</span>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div v-else style="height: 90%;
    display: flex;
    justify-content: center;
    align-items: center;">
      <el-empty
        :image="emptyImage"
        description="暂无数据"
        :image-size="100"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { ArrowDown } from '@element-plus/icons-vue';
import emptyImage from '@/assets/icon/internet_empty_illustration.svg';

const props = defineProps<{
  patientId: number | null;
  patientDetail: any;
  activeTab?: string;
}>();

const activeTab = ref(props.activeTab);
// 折叠面板控制
const activeNames = ref(['1']);
const isExpanded = ref(true); // 控制折叠面板显示/隐藏

// 切换折叠状态
const toggleCollapse = () => {
  isExpanded.value = !isExpanded.value;
};
</script>

<style scoped lang="scss">
.follow-up-card {
  background: #ffffff;
  border-radius: 8px;
  width: 280px;
  height: calc(100% - 40px);
  overflow: auto;
  margin-top: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.card-title {
  width: 280px;
  height: 48px;
  border-radius: 0px 0px 0px 0px;
  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 600;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.9);
  line-height: 22px;
  padding-left: 17px;
  line-height: 48px;
  border-bottom: 1px solid #dddddd;
}

.diagnosis-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e7e7e7;
}

.diagnosis-name {
  font-size: 16px;
  color: #666;
}

.doctor-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: opacity 0.2s ease;
}

.doctor-info:hover {
  opacity: 0.8;
}

.doctor-name {
  font-size: 14px;
  color: #666;
}

.visit-date {
  font-size: 14px;
  color: #666;
}

.expand-icon {
  font-size: 12px;
  color: #666;
  transition: transform 0.3s ease;
}

.expand-icon.rotated {
  transform: rotate(180deg);
}

.follow-up-collapse {
  border: none;
  padding: 0px 8px 10px 8px;
}

:deep(.el-collapse-item__header) {
  border: none;
  border-radius: 6px;
}

:deep(.el-collapse-item__arrow) {
  transition: transform 0.3s ease;
  transform: rotate(-90deg); /* 默认状态(关闭时)向上 */
}

:deep(.el-collapse-item__arrow.is-active) {
  transform: rotate(90deg); /* 展开状态(打开时)向下 */
}

:deep(.el-collapse-item__content) {
  padding: 0px 8px 16px 8px;
  background: #f8f8f8;
  border-radius: 0 0 6px 6px;
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #dcdcdc;
}

.collapse-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  font-weight: bold;
  font-size: 11px;
  color: rgba(0, 0, 0, 0.6);
  line-height: 22px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

.diagnosis {
  font-weight: bold;
font-size: 11px;
color: rgba(0,0,0,0.6);
}

.action-btn {
  font-size: 14px;
  color: #00b3b3;
  cursor: pointer;
}

.collapse-content {
  font-size: 11px;
  line-height: 1.6;
  padding: 0px 0 10px 0;
  border-bottom: 1px dashed #dcdcdc;
}



.label {
  color: #666;
  margin-right: 8px;
  min-width: 80px;
  flex-shrink: 0;
  font-size: 11px;
}

.value {
  color: #333;
  flex: 1;
}
.tcm-box {
  display: flex;
  flex-wrap: wrap;

  gap: 0;
}
.tcm-box .info-row {
  width: 33.33%;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-bottom: 0;
  padding: 8px 0;
}
.tcm-box .label {
  font-size: 11px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 4px;
}
</style>
